<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/common/util/common.php';
/* todo
 * 클래스, 메서드 정보를 담고있는 테이블 조회
 * 클래스 선언 및 메서드 실행
 * 리턴 결과 전달 
 * */
class service{ 
	private $fpBatis;

	function __construct(){
		$this->fpBatis = new FPBatis(SQL_MAP_URL);
		$this->fpBatis->setDebug(false);
	}
	
	public function processService(){
		$dbjob = $_POST['dbjob'];
		$methodNm;
		switch($dbjob){
			case 'S' :
				$methodNm = "selectServiceList";	
			break;
			
			case 'I' :
				$methodNm = "insertService";
			break;
			
			case 'D' : 
				$methodNm = "deleteService";
			break;
		}
		$func = array('service', $methodNm);
		//print_r($func);
		$result = call_user_func($func, $_POST);
		
		
		if(is_array($result)){
			$result = encode_items($result);
			echo urldecode(json_encode($result));
		}else{
			echo 'success';				
		}
	}
	
	private function selectServiceList($param){
		$data = array();
		$data['serviceList'] = $this->fpBatis->doSelect("Service.selectServiceList",$param); 
		return $data;
	}
	
	private function insertService($param){
		$data = $param['data'];
		foreach($data as $k=>$v){
			$dbParam = array();
			$dbParam['serviceId'] = $v['serviceId'];
			$dbParam['classNm'] = $v['classNm']; 
			$dbParam['methodNm'] = $v['methodNm']; 
			$dbParam['comments'] = $v['comments'];

			$cnt = $this->fpBatis->doSelect("Service.selectService",$dbParam);
			if($cnt[0]['CNT'] > 0){
				$this->fpBatis->doUpdate("Service.updateService",$dbParam);
			}else{
				$this->fpBatis->doInsert("Service.insertService",$dbParam);
			}
		}
		return 	"i";
	}
	
	private function deleteService($param){
		$dbParam = array();
		$dbParam['serviceId'] = $param['data']['serviceId'];
		
		$this->fpBatis->doDelete("Service.deleteService", $dbParam);
		
		return 	"d";
	}
	
}

$service = new service();
$service->processService();
?>